﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;


namespace SqlMiM.Common.Model
{
    public class Index
    {
        /// <summary>
        /// Gets the Index Object ID  (Table View)
        /// </summary>
        public int ObjectId { get; set; }

        /// <summary>
        /// Index Object Name (Table, View)
        /// </summary>
        public String ObjectName { get; set; }

        /// <summary>
        /// Gets the index ID 
        /// </summary>
        public int IndexId { get; set; }

        /// <summary>
        /// Index Name
        /// </summary>
        public String IndexName { get; set; }

        /// <summary>
        /// Database Id
        /// </summary>
        public Int32 DatabaseId { get; set; }

        /// <summary>
        /// Database Owner
        /// </summary>
        public String DatabaseName { get; set; }

        /// <summary>
        /// Index Type Id 
        /// </summary>
        public Byte IndexType { get; set; }

        /// <summary>
        /// Index Type Description
        /// </summary>
        public String IndexTypeDescription { get; set; }

        /// <summary>
        /// Gets or sets the percentage of an index page to fill when the index is created or re-created.
        /// </summary>
        public byte FillFactor { get; set; }

        /// <summary>
        /// True, the index is clustered.If False (default), the index is nonclustered.
        /// </summary>
        public bool IsClustered { get; set; }

        /// <summary>
        /// True if the index is disabled.If False (default), the index is enabled.
        /// </summary>
        public bool IsDisabled { get; set; }

        /// <summary>
        /// If True, the index is enabled for Microsoft
        ///     Search row identification.If False (default), the index is not enabled for
        ///     Microsoft Search row identification.
        /// </summary>
        public bool IsFullTextKey { get; set; }

        //
        // Summary:
        //     Gets the System.Boolean property value that specifies whether the index is
        //     defined for a table or a view.
        //
        public bool IsIndexOnTable { get; set; }

        //
        // Summary:
        //     Gets or sets the System.Boolean property value that specifies whether the
        //     index is unique or not.
        //
        public bool IsUnique { get; set; }
        //
        // Summary:
        //     Gets or sets the System.Boolean property value that specifies whether the
        //     index is an XML index.
        //
        public bool IsXmlIndex { get; set; }

        //
        // Summary:
        //     Gets or sets the System.Boolean property value that specifies whether space
        //     is left open on each page of the intermediate levels of the index.
        //
        public bool PadIndex { get; set; }
 
  
        public static void FillFromReader(SqlDataReader reader, Index index)
        {
            index.FillFactor = reader["fill_factor"] != System.DBNull.Value ? (Byte)reader["fill_factor"] : (Byte)0;
            index.IndexId = (Int32)reader["index_id"];
            index.IndexName = reader["name"] as String;
            index.IndexType = reader["type"] != System.DBNull.Value ? (Byte)reader["type"] : (Byte)0;
            index.IndexTypeDescription = reader["type_desc"] as String;
            index.IsClustered = index.IndexId == 1 ? true : false;
            index.IsDisabled = (Boolean)reader["is_disabled"];
            index.IsFullTextKey = (Boolean)reader["is_full_text_key"];
            index.IsIndexOnTable = true;
            index.IsUnique = (Boolean)reader["is_unique"];
            index.IsXmlIndex = index.IndexType == 3 ? true : false;
            index.ObjectId = (Int32)reader["object_id"];
            index.ObjectName = reader["object_name"] as String;
            index.PadIndex = (Boolean)reader["pad_index"];
        }

    }
}
